/*
** pud_call.c for pud in mrcellophane/libpud/sys/NetBSD
** 
** Made by solomon hykes
** Login   <torgau@gmail.com>
** 
** Started on  Sun Jun  5 01:00:21 2005 solomon hykes
** Last update Sun Jun  5 02:41:48 2005 solomon hykes
*/

#include "pud_sys.h"

// FIXME: We don't check if we have enough space to write the code

t_ret	pud_code_exec(pud_process *p, void *code, u_long codeLen)
{
  sys_pud_process	*proc = (sys_pud_process *)p;
  void			*codeSave;
  u_long		pcSave;

  /* Save current code before we re-write it */
  codeSave = malloc(codeLen);
  pud_mem_read(p, proc->pc, codeSave, codeLen);

  /* Write the new code */
  pud_mem_write(p, proc->pc, code, codeLen);

  /* Save PC */
  pcSave = proc->pc;

  /* EXECUTE CODE */
  do
    {
      pud_step(p, 0);
      pud_next_event(p);
    }
  while ((proc->pc >= pcSave) && (proc->pc < pcSave + codeLen));

  /* Restore saved code */
  pud_mem_write(p, pcSave, codeSave, codeLen);
  free(codeSave);
  return (ret_ok);
}
